package com.red.star.wechat.data.utils;

import org.apache.http.HttpException;
import org.apache.http.client.fluent.Request;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 芝麻接口
 */
public class SesameUtil {

    //部署域名
    private static String SesameUrl = "http://bit.zhimatech.com";

    //开放平台⽤户登录
    private static String loginUrl = "/magic-api/v1/user/open-login";

    //设备状态
    private static String deviceStatusUrl = "/magic-api/v1/device/status";

    //设备上下线日志
    private static String deviceOnLineLogUrl = "/magic-api/v1/device/onlinelog";

    //操作日志
    private static String operateLogUrl = "/magic-api/v1/log/major-list";

    //AppId
    private static String appid = "3a95d1e6c08b11e8a579027f90066cee";

    //secret
    private static String secret = "a8bde78555e04257a3553be9f00287e1";

    //烟台source
    private static String yanTaiSource = "27f2250a9aeb11e8acf1027f90066cee";


    //token_type
    private static String token_type = "Bearer";
    //access_token
    private static String access_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImEzZWZjYzY1NDI1ODRlNzljMWMwYmQ0NjI1ZDMyYTMxNDFmNzMyYWNhNDM5ZjU4ZWM2NTZmMGI4NTY3Nzc5YmIxY2Q3Y2Q4ZjI3ZjEwMmZhIn0.eyJhdWQiOiIyIiwianRpIjoiYTNlZmNjNjU0MjU4NGU3OWMxYzBiZDQ2MjVkMzJhMzE0MWY3MzJhY2E0MzlmNThlYzY1NmYwYjg1Njc3NzliYjFjZDdjZDhmMjdmMTAyZmEiLCJpYXQiOjE1NDAxNzc1MzksIm5iZiI6MTU0MDE3NzUzOSwiZXhwIjoxNTcxNzEzNTM5LCJzdWIiOiI0NzUzIiwic2NvcGVzIjpbXX0.BxI_bolUtFuza14O5pnOM8unylmDk6nyqO9-ewgM9uO-7udyXsv6IT2vHGKqJoTF1h1fSx1IC5U_CbA8nXS3UQvqmijRLcDJytk0wT4fiudXwY6olXUUJ6veo-1H7K143v2h2MxfyDOfo9cry0o9KMnsF8-qYISZ8PlVhPG4ljq7M-wzMce3SA37-Gk9ibER8LoGnlQ5sFj_p9A3yZPuSbSOrc9n2AGwDCLqeNC7FaK0aOq8Fp5mzdMDcrkaPKUjwZBtTTOEcZraePwL1Z6Zsdu2iV2YtHF_bqpW7caQbMFIVaIb_4s8ajGixHh53dnFr9H3I5WIDyISgriQQj9XUWm8zSbae-0jo4DOntymvo50NDGgujMAbJ0M5r8CDNu0HX30XJ6c9N_yupRVuvyF1n4Dm6MuxYS0elO6C9A84PeQAbjONGcxafFjiHWboSXLJkEiMoz5M1E9RYbwaR5xptp0Zs9G8MDAlOL0ZwFm6trXOBd118ZSdlQbzFY7t7rJoTPXvomDGhl7EwG25yu02YujE5jNC9Wucr1rCjkAaZ03ALIEy-p9f2ezHJHVmbo8J-_bbDNnfqAgKDokREOfeDurY5ng8VYrSdGW6f1Mc6RhjCDfj1C9igWTmMNoPQE_nHrmBF0iijs48RKtlMK6MF3ESGNFE0uz83ICGEN-dTc";


    public static String getLogin(String source) throws IOException, HttpException, URISyntaxException {
//        source = yanTaiSource;
        Map<String,String> map = new HashMap<>();
        map = getTime();
        String timestamp = map.get("timestamp");
        String ymdHi = map.get("ymdHi");
        String url = SesameUrl+loginUrl;
        String param = "appid="+appid
                +"&source="+source
                +"&timestamp="+timestamp
                +"&signature="+getSignature(source,ymdHi);

        System.out.println("请求地址=====>" + url);
        System.out.println("请求参数=====>" + param);
        String path = url + "?" + param;
        System.out.println("请求连接=====>" + path);
        Request request = Request.Get(path);
        return HttpUtil.fetch(request);
    }


    private static Map<String,String> getTime()  throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        long date = System.currentTimeMillis();
        Map<String,String> map = new HashMap<>();
        map.put("timestamp",date / 1000 + "");
        map.put("ymdHi",sdf.format(date));
        return  map;
    }

    private static String getSignature(String source,String ymdHi)  throws IOException {
        String signature = "";
        System.out.println("appid===》" + appid);
        System.out.println("source===》" + source);
        System.out.println("YmdHi===》" + ymdHi);
        System.out.println("secret===》" + secret);
        String param = appid+source+ymdHi+secret;
        System.out.println("拼接后参数===》"+param);
        signature = SHA1Util.getSha1(appid+source+ymdHi+secret);
        System.out.println("加密后签名===》"+signature);
        return signature;
    }


    //查看设备状态
    public static String deviceStatus(String deviceId){
        String url = SesameUrl+deviceStatusUrl;
        String param = "deviceid=" + deviceId;
        System.out.println("请求地址=====>" + url);
        System.out.println("请求参数=====>" + param);
        String path = url + "?" + param;
        System.out.println("请求连接=====>" + path);
        Request request = Request.Get(path);
        request.setHeader("Authorization",token_type+" "+access_token);
        System.out.println("请求header=====>" + token_type+" "+access_token);
        return HttpUtil.fetch(request);
    }

    //设备上下线⽇志
    public static String deviceOnlineLog(String deviceId,String page,String pageSize){
        String url = SesameUrl+deviceOnLineLogUrl;
        String param = "page=" + page
                +"&page_size=" + pageSize
                +"&deviceid=" + deviceId;
        System.out.println("请求地址=====>" + url);
        System.out.println("请求参数=====>" + param);
        String path = url + "?" + param;
        System.out.println("请求连接=====>" + path);
        Request request = Request.Get(path);
        request.setHeader("Authorization",token_type+" "+access_token);
        System.out.println("请求header=====>" + token_type+" "+access_token);
        return HttpUtil.fetch(request);
    }

    //操作日志
    public static String getOperateLog(String page,String pageSize,String system,String type){
        String url = SesameUrl+operateLogUrl;
        String param = "page=" + page
                +"&page_size=" + pageSize
                +"&system=" + system
                +"&type=" + type;
        System.out.println("请求地址=====>" + url);
        System.out.println("请求参数=====>" + param);
        String path = url + "?" + param;
        System.out.println("请求连接=====>" + path);
        Request request = Request.Get(path);
        request.setHeader("Authorization",token_type+" "+access_token);
        System.out.println("请求header=====>" + token_type+" "+access_token);
        return HttpUtil.fetch(request);
    }



    //Unicode转中文
    public static String unicodeToCn(String unicode) {
        /** 以 \ u 分割，因为java注释也能识别unicode，因此中间加了一个空格*/
        String[] strs = unicode.split("\\\\u");
        String returnStr = "";
        // 由于unicode字符串以 \ u 开头，因此分割出的第一个字符是""。
        for (int i = 1; i < strs.length; i++) {
            returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
        }
        return returnStr;
    }

    //中文转Unicode
    public static String cnToUnicode(String cn) {
        char[] chars = cn.toCharArray();
        String returnStr = "";
        for (int i = 0; i < chars.length; i++) {
            returnStr += "\\u" + Integer.toString(chars[i], 16);
        }
        return returnStr;
    }
}
